package cn.mw.cmdb.listener;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.param.ContextAddListener;
import cn.mw.cmdb.param.ContextDeleteListener;
import cn.mw.cmdb.param.ContextEditorListener;
import cn.mw.cmdb.service.impl.AssetsManageServiceImpl;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.InstanceServiceImpl;
import cn.mw.microMonitorCommon.util.SpringUtils;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.mw.cmdb.service.impl.InstanceServiceImpl.beforeInstanceKey;

/**
 * 监控服务器数据同步
 */
@Service
@Slf4j
public class MonitorServerSyncHandle implements InstanceChangeEventLister {


    @Override
    public void addReceive(ContextAddListener contextListener) throws Exception {

    }

    @Override
    public Object editorReceive(ContextEditorListener contextListener) throws Exception {
        log.info("into-MonitorServerSyncHandle-editorReceive::33");
        CmdbServiceManage cmdbServiceManage = SpringUtils.getBean(CmdbServiceManage.class);
        InstanceServiceImpl instanceService = (InstanceServiceImpl) cmdbServiceManage.getModelService(InstanceServiceImpl.ID);
        Map<String, Object> map = contextListener.getMap();
        Object beforeInstanceObj = map.get(beforeInstanceKey);
        InstanceInfo beforeInstance = new InstanceInfo();
        if (beforeInstanceObj != null && beforeInstanceObj instanceof InstanceInfo) {
            beforeInstance = (InstanceInfo) beforeInstanceObj;
        }
        Object param = contextListener.getParam();
        if (param != null && param instanceof List<?>) {
            List<InstanceInfo> instanceInfoList = (List<InstanceInfo>) param;
            if (CollectionUtils.isNotEmpty(instanceInfoList) && instanceInfoList.size() > 0) {
                InstanceInfo instanceInfo = instanceInfoList.get(0);
                Integer monitorServerId = instanceInfo.getMonitorServerId();
                String hostId = instanceInfo.getMonitorTypeId();
                Boolean trunkMonitorFlagKey = instanceInfo.getTrunkMonitorFlagKey();
                if (monitorServerId != null && monitorServerId.intValue() != 0 && !Strings.isNullOrEmpty(hostId) && !"0".equals(hostId)) {
                    //轮询引擎变更同步修改zabbix数据
                    if (beforeInstance != null && instanceInfo.getPollingEngine() != null && !instanceInfo.getPollingEngine().equals(beforeInstance.getPollingEngine())) {
                        instanceService.batchUpdateMonitorProxy(instanceInfoList, instanceInfo.getProxyType(), instanceInfo.getPollingEngine());
                    }
                    //IP地址变更同步修改zabbix数据
                    if (beforeInstance != null && instanceInfo.getInBandIp() != null && !instanceInfo.getInBandIp().equals(beforeInstance.getInBandIp())) {
                        instanceService.updateHostMonitorIpInfo(instanceInfo);
                    }

                    //trunk监控修改
                    if (beforeInstance != null && trunkMonitorFlagKey != null && !trunkMonitorFlagKey.equals(beforeInstance.getTrunkMonitorFlagKey())) {
                        AssetsManageServiceImpl bean = SpringUtils.getBean(AssetsManageServiceImpl.class);
                        bean.batchEditorInterfaceMonitor(Arrays.asList(instanceInfo.getId()), trunkMonitorFlagKey);
                    }
                }
            }
        }


        return null;
    }

    @Override
    public void deleteReceive(ContextDeleteListener contextListener) throws Exception {

    }
}
